/*
 * 资讯管理
 */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { message,Button, Spin, Form, Input, Table,Modal,Upload,Icon } from 'antd'
import '../../style/industryVideo.less'

import {
  fetchVideoCheckList,addVideoinfo,deleteVideoinfo
} from 'actions/industry'

import ModalAddIndustryVideo from './addIndustryVideoModal.js';


if (process.env.NODE_ENV === 'dev') {
  console.log('dev')
}

//123456789
const FormItem = Form.Item

@Form.create({})

@connect(
  (state, props) => ({
    config: state.config,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",
      addModalFlg:false,
      modalList:"",
      addModalType:"",
    }
    this.keyword={"title":"","publisherName":""}
    this.currentNumber = 1;
    this.pagesize = 10;
  }

  componentDidMount() {//页面首次加载
    this.selectList();
  }
  searchFN(){//点击搜索按钮
    this.keyword = {"title":this.props.form.getFieldValue('title'),"publisherName":this.props.form.getFieldValue('publisherName')};
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(fetchVideoCheckList(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }

  addReport(type,data){//新增视频
    //编辑的情况  带数据去组件
    this.setState({
      addModalFlg: true,
      modalList:data,
      addModalType:type,
    })
  }
  addModalCancel(){
    this.setState({
      addModalFlg: false,
    });
  }

  deleteList(id){
    Modal.confirm({
      content:"确定要删除该条信息么？",
      onOk:()=>{
        this.props.dispatch(deleteVideoinfo(
          {"contentId":id},
          (respose) => {
            var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
            if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
              this.currentNumber = this.currentNumber - 1;
            }
            message.success(respose.respMsg);
            this.selectList(this.currentNumber);
        }))
      },
      onCancel:()=>{
      }
    })
  }
  
  columns() {
    return [
      {
        title: '视频编号',
        key: 'code',
        dataIndex: 'code',        
        width: '10%',
      },
      {
        title: '视频标题',
        dataIndex: 'title',
        key: 'title',
        width: '15%',
      },
      {
        title: '封面图',
        dataIndex: 'imageAddr',
        key: 'imageAddr',
        width: '10%',
        render: (text, recordId, index) => <img style={{width:'50px'}} src={text ? text : require('../../images/baseHeader.png')} />,
      },
      {
        title: '发布人',
        dataIndex: 'publisherName',
        key: 'publisherName',
        width: '10%',
      },
      {
        title: '发布时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '15%',
      },
      {
        title: '阅读数',
        dataIndex: 'readTotal',
        key: 'readTotal',
        width: '10%',
      },
      {
        title: '评论数',
        dataIndex: 'commentTotal',
        key: 'commentTotal',
        width: '10%',
      },
      {
        title: '点赞数',
        dataIndex: 'fabulousTotal',
        key: 'fabulousTotal',
        width: '10%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          <Button type="primary" size="small" onClick={this.addReport.bind(this,2,text)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>          
          <Button type="primary" size="small" onClick={this.deleteList.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>删除</Button>  
        </div>,
      },
    ]
  }

  
  
  render() {
    const { houseCheckSearchResult, form } = this.props;
    const { getFieldDecorator } = form
     
    const { TextArea } = Input;
    const videoCheckList = this.state.list ?this.state.list.respData:[] ;
    return (
      <div className="page">
        <div className="search" style={{ marginBottom: '10px' }}>
          <Form onSubmit={this.searchFN.bind(this)} layout="inline">
            <FormItem label="视频标题">
              {
                getFieldDecorator('title', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入视频标题" size="default" style={{ width: '200px' }} />
                )
              }
            </FormItem>
            <FormItem label="发布人">
            {
              getFieldDecorator('publisherName', {
                rules: [{
                  required: false,
                }],
              })(
                <Input placeholder="请输入发布人" size="default" style={{ width: '200px' }} />
              )
            }
          </FormItem>
            <Button type="primary" onClick={this.searchFN.bind(this)}>搜索</Button>
            <Button type="primary" className="addvideobtn" onClick={this.addReport.bind(this,1,{})}>新增</Button>
          </Form>
        </div>
          <Table
            rowKey="id"
            dataSource={videoCheckList.resultData}
            columns={this.columns()}
            pagination={{ total: videoCheckList.count, current:this.currentNumber, pageSize: this.pagesize, onChange:this.selectList.bind(this)}}
            scroll={{ y: true }}
            rowClassName={(record, index) => record.id === 1 ? 'red' : 'base'}
          />
        {/* 添加视频 */}
        {
          this.state.addModalFlg ? 
          <ModalAddIndustryVideo 
            type={this.state.addModalType}
            ModalFlg={this.state.addModalFlg} 
            ModalCancel={this.addModalCancel.bind(this)} 
            callback={this.selectList.bind(this)}
            content={this.state.modalList.id ? this.state.modalList : {}}
          /> :''
        }
        
      </div>
    )
  }
}
